package yxy.game.pm2.command.union.leader;

import yxy.apple.util.concurrent.ThreadPoolManager;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.area.AreaGroupBean;
import yxy.game.pm2.bean.cluster.ClusterBean;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.bean.module.rank.openserviceactive.boosrushrank.OpenServiceBoosRushRank;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionLeaderMessage;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.UnionName;
import yxy.game.pm2.module.union.player.UnionPlayer;
import yxy.game.pm2.module.union.rank.*;

/** 联盟盟主 联盟解散 */
@MessageType(proto = Command.UnionDismiss, classtype = UnionLeaderMessage.UnionDismissRequest.class)
public class CMD_UnionDismiss extends Command<UnionLeaderMessage.UnionDismissRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionLeaderMessage.UnionDismissRequest request) {
		String playerCode = messageSource.getTag();

		UnionLeaderMessage.UnionDismissResponse.Builder response = UnionLeaderMessage.UnionDismissResponse.newBuilder();

		// 盟主权限
		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		Result result = new UnionHandle().checkPlayerUnionLeader(entry);
		if (!result.equals(Result.UnionLeader)) {
			send(messageSource, response, result);
			return;
		}
		RedisLock uLock = UnionBean.lock(entry.getUnionBean().getCode());
		try {
			if (uLock.tryLock()) {
				UnionBean union = entry.reloadUnionBean();

				// 重置成员
				for (String code : union.members()) {
					RedisLock pLock = UnionPlayer.lock(code);
					try {
						if (pLock.tryLock()) {
							UnionPlayer unionPlayer = UnionPlayer.get(code);
							unionPlayer.setUnionCode(null);
							unionPlayer.set();

							String oppoCode = unionPlayer.getPlayerCode();
							PlayTypeHandle.saveUnion(oppoCode, PlayTypeModule.get(oppoCode));
							new BattleHandle().powerUpdate(oppoCode);

							// 联盟状态推送
							new UnionHandle().unionPlayerNotification(unionPlayer.getPlayerCode());
						}
					} catch (Exception e) {
					} finally {
						pLock.unlock();
					}
				}
				// 删除名录
				UnionName.getInstance().delete(union);

				ServerBean serverBean = ServerBean.get(union.getServerId());
				AreaBean areaBean = AreaBean.get(serverBean.getAreaId());
				AreaGroupBean areaGroupBean = AreaGroupBean.get(areaBean.getGroupId());
				ClusterBean clusterBean = ClusterBean.get(areaBean.getClusterId());
				// 删除排行榜
				new RankActiveUnion(clusterBean.getClusterId()).remove(union.getCode());
				new RankLevelUnion(clusterBean.getClusterId()).remove(union.getCode());
				new RankPowerUnion(clusterBean.getClusterId()).remove(union.getCode());

				Runnable runnable = () -> {
					//删除开服冲榜排行榜
					OpenServiceBoosRushRank.deleteBoosRanking(playerCode,union.getCode(),1004);
				};
				ThreadPoolManager.getInstance().execute(runnable);
				RankAreaActiveUnion.get(union.getServerId()).remove(union.getCode());
				RankAreaLevelUnion.get(union.getServerId()).remove(union.getCode());
				RankAreaPowerUnion.get(union.getServerId()).remove(union.getCode());

				RankAreaGroupActiveUnion.get(areaGroupBean.getGroupId()).remove(union.getCode());
				RankAreaGroupLevelUnion.get(areaGroupBean.getGroupId()).remove(union.getCode());
				RankAreaGroupPowerUnion.get(areaGroupBean.getGroupId()).remove(union.getCode());

				// 删除本体
				UnionBean.remove(union.getCode());

				send(messageSource, response, Result.Success);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			uLock.unlock();
		}
	}

	private void send(MessageSource messageSource, UnionLeaderMessage.UnionDismissResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}